﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SimpleExpression
{
    public class PropertyMetadataCollection : List<PropertyMetadata>
    {
        public PropertyMetadataCollection()
        { 
            
        }

        public PropertyMetadata this[string name, MappingType mappingType = MappingType.Physical]
        {
            get
            {
                if (mappingType == MappingType.Physical)
                    return this.Where(c => c.PhysicalName == name).First();
                else
                    if (mappingType == MappingType.Platform)
                        return this.Where(c => c.PlatformName == name).First();
                    else
                        throw new KeyNotFoundException();
            }
        }

        public PropertyMetadata this[Guid id]
        {
            get
            {
                return this.Where(c => c.PropertyId == id).First();
            }
        }

        public IEnumerable<string> Keys
        {
            get
            {
                return this.Select(c => c.PhysicalName);
            }
        }

        public bool Contains(string name, MappingType mappingType)
        {
            switch (mappingType)
            {
                case MappingType.None:
                    break;
                case MappingType.Physical:
                    return this.Where(c => c.PhysicalName == name).Count() > 0;
                case MappingType.Platform:
                    return this.Where(c => c.PlatformName == name).Count() > 0;
                default:
                    break;
            }

            return false;
        }

        public MappingType Contains(string name)
        {
            if (Contains(name, MappingType.Physical))
                return MappingType.Physical;
            else
                if (Contains(name, MappingType.Platform))
                    return MappingType.Platform;
                else
                    return MappingType.None;
        }
    }
}
